package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpProductAtlasTag;
import com.erp.gather.domain.bo.ErpProductAtlasTagBo;
import com.erp.gather.domain.vo.ErpProductAtlasTagVo;
import com.erp.gather.mapper.ErpProductAtlasTagMapper;
import com.erp.gather.service.IErpProductAtlasTagService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 图集标签Service业务层处理
 *
 * @author erp
 * @date 2023-03-21
 */
@RequiredArgsConstructor
@Service
public class ErpProductAtlasTagServiceImpl implements IErpProductAtlasTagService {

    private final ErpProductAtlasTagMapper baseMapper;

    /**
     * 查询图集标签
     */
    @Override
    public ErpProductAtlasTagVo queryById(Long atlasTagId){
        return baseMapper.selectVoById(atlasTagId);
    }

    /**
     * 查询图集标签列表
     */
    @Override
    public TableDataInfo<ErpProductAtlasTagVo> queryPageList(ErpProductAtlasTagBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpProductAtlasTag> lqw = buildQueryWrapper(bo);
        Page<ErpProductAtlasTagVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询图集标签列表
     */
    @Override
    public List<ErpProductAtlasTagVo> queryList(ErpProductAtlasTagBo bo) {
        LambdaQueryWrapper<ErpProductAtlasTag> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpProductAtlasTag> buildQueryWrapper(ErpProductAtlasTagBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpProductAtlasTag> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTagName()), ErpProductAtlasTag::getTagName, bo.getTagName());
        return lqw;
    }

    /**
     * 新增图集标签
     */
    @Override
    public Boolean insertByBo(ErpProductAtlasTagBo bo) {
        ErpProductAtlasTag add = BeanUtil.toBean(bo, ErpProductAtlasTag.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAtlasTagId(add.getAtlasTagId());
        }
        return flag;
    }

    /**
     * 修改图集标签
     */
    @Override
    public Boolean updateByBo(ErpProductAtlasTagBo bo) {
        ErpProductAtlasTag update = BeanUtil.toBean(bo, ErpProductAtlasTag.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpProductAtlasTag entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除图集标签
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
